﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;

namespace DailyBackupService
{
    partial class DailyBackup : ServiceBase
    {
        System.Timers.Timer TimeWatcher = null;        
        public DailyBackup()
        {
            try
            {
                InitializeComponent();
                TimeWatcher = new System.Timers.Timer(Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["WatchInterval"]));
                TimeWatcher.AutoReset = true;
                TimeWatcher.Elapsed += new System.Timers.ElapsedEventHandler(TimeWatcher_Elapsed);
            }
            catch (Exception ex) { }
        }

        protected override void OnStart(string[] args)
        {            
            // TODO: Add code here to start your service.
            try
            {
                TimeWatcher.Start();                  
            }
            catch (Exception ex) { }
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            try
            {
                TimeWatcher.Stop();
            }
            catch (Exception ex) { }
        }

        public void TimeWatcher_Elapsed(object sender, System.Timers.ElapsedEventArgs  e)
        {
            try
            {
                GenerateBackup.Create();
            }
            catch (Exception ex) { }
        }
    }
}
